package com.meter.data.service.impl;

import java.util.*;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.meter.common.config.datasource.DynamicDataSourceContextHolder;
import com.meter.common.enums.DataSourceType;
import com.meter.common.handler.MyTableNameHandler;
import com.meter.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meter.common.annotation.DataSource;
import com.meter.config.domain.ConfigAlarm;
import com.meter.config.mapper.ConfigAlarmMapper;
import com.meter.data.domain.MeterData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.meter.data.mapper.AlarmDataMapper;
import com.meter.data.domain.AlarmData;
import com.meter.data.service.IAlarmDataService;

/**
 * 告警记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-15
 */
@Service
public class AlarmDataServiceImpl extends ServiceImpl<AlarmDataMapper, AlarmData> implements IAlarmDataService
{

    @Autowired
    private ConfigAlarmMapper configAlarmMapper;

    /**
     * 查询告警记录
     * 
     * @param recordId 告警记录主键
     * @return 告警记录
     */
    @Override
    @DataSource(DataSourceType.SLAVE)
    public AlarmData selectAlarmDataByRecordId(Long recordId)
    {
        return baseMapper.selectAlarmDataByRecordId(recordId);
    }

    /**
     * 查询告警记录列表
     * 
     * @param alarmData 告警记录
     * @return 告警记录
     */
    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<AlarmData> selectAlarmDataList(AlarmData alarmData)
    {
        String beginTime = alarmData.getParams().get("beginTime").toString();
        String endTime = alarmData.getParams().get("endTime").toString();
        String beginMonth = beginTime.isEmpty() ? DateUtils.getMonth(new Date()) : DateUtils.getMonth(beginTime);
        String endMonth = endTime.isEmpty() ? DateUtils.getMonth(new Date()) : DateUtils.getMonth(endTime);
        List<AlarmData> list = new ArrayList<>();
        while (DateUtils.parseMonth(endMonth).getTime() >= DateUtils.parseMonth(beginMonth).getTime()) {
            MyTableNameHandler.setData(beginMonth);
            List<AlarmData> alarmDataList = baseMapper.selectAlarmDataList(alarmData);
            list.addAll(alarmDataList);
            MyTableNameHandler.removeData();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(DateUtils.parseMonth(beginMonth));
            calendar.add(Calendar.MONTH, 1);
            beginMonth = DateUtils.getMonth(calendar.getTime());
        }
        return list;
    }

    /**
     * 新增告警记录
     * 
     * @param meterData 告警记录
     * @return 结果
     */
    @Override
    public void insertAlarmData(MeterData meterData)
    {
        List<ConfigAlarm> configAlarmList = configAlarmMapper.selectList(Wrappers.emptyWrapper());
        for(ConfigAlarm configAlarm : configAlarmList) {
            double diffValue = meterData.getUseFlow();
            if(configAlarm.getAlarmField().equals("H")) {
                diffValue = meterData.getUseHeat();
            }
            AlarmData alarmData = new AlarmData();
            alarmData.setMeterId(meterData.getMeterId());
            alarmData.setAlarmId(configAlarm.getAlarmId());
            alarmData.setDiffValue(diffValue);
            if(configAlarm.getAlarmType().equals("01")) {
                if(configAlarm.getAlarmValue() > diffValue) {
                    alarmData.setRemark("热表：" + meterData.getMeterNo() + "高于预警值：" + configAlarm.getAlarmValue());
                    alarmData.setCreateTime(DateUtils.getNowDate());
                    DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
                    baseMapper.insert(alarmData);
                    DynamicDataSourceContextHolder.clearDataSourceType();
                }
            } else {
                if(configAlarm.getAlarmValue() < diffValue) {
                    alarmData.setRemark("热表：" + meterData.getMeterNo() + "低于预警值：" + configAlarm.getAlarmValue());
                    alarmData.setCreateTime(DateUtils.getNowDate());
                    DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
                    baseMapper.insert(alarmData);
                    DynamicDataSourceContextHolder.clearDataSourceType();
                }
            }
        }
    }
}
